#include "XRectangle.h"

/**
Constructeur
*/
XRectangle::XRectangle()
{
	_pVB = NULL;
	_pTexture = NULL;
	//_filename = imgFilename;
	_pEventPlace = NULL;
	_bToDel = false;
}

/**
Descrupteur
*/
XRectangle::~XRectangle(void)
{
	if ( _pTexture )
		_pTexture->Release();
	if ( _pVB )
		_pVB->Release();
}

/**
Affiche un Album
@param[in]	pDevice pointeur sur un device D3D
*/
void XRectangle::draw(LPDIRECT3DDEVICE9 pDevice)
{
	if ( _iTrans > 0 )
	{
		_iTrans--;
		_x += _txStep;
		_rot += _trotStep;
	}
	translate( pDevice );
	pDevice->SetTexture( 0, _pTexture );
    // Render the vertex buffer contents
    pDevice->SetStreamSource( 0, _pVB, 0, sizeof(CUSTOMVERTEX) );
    pDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

    pDevice->SetStreamSource( 0, _pVB2, 0, sizeof(CUSTOMVERTEX) );
    //pDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
	pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );

}

/**
Creation d'un XRectangle
@param[in]	pDevice	pointeur sur un device D3D
*/
HRESULT XRectangle::create(LPDIRECT3DDEVICE9 pDevice)
{
	_iTrans = 0;
    CUSTOMVERTEX g_Vertices[] =
    {
        { D3DXVECTOR3( 1.0f, 1.0f, 0.0f), 0xffffffff, 1.0f, 0.0f},
        { D3DXVECTOR3( 1.0f,-1.0f, 0.0f), 0xffffffff, 1.0f, 1.0f},
        { D3DXVECTOR3(-1.0f, 1.0f, 0.0f), 0xffffffff, 0.0f, 0.0f},
		{ D3DXVECTOR3(-1.0f,-1.0f, 0.0f), 0xffffffff, 0.0f, 1.0f},
    };
    CUSTOMVERTEX g_Vertices2[] =
    {
        { D3DXVECTOR3( 1.0f,-1.0f, 0.0f), 0x40ffffff, 1.0f, 1.0f},
        { D3DXVECTOR3( 1.0f,-3.0f, 0.0f), 0x00000000, 1.0f, 0.0f},
        { D3DXVECTOR3(-1.0f,-1.0f, 0.0f), 0x40ffffff, 0.0f, 1.0f},
		{ D3DXVECTOR3(-1.0f,-3.0f, 0.0f), 0x00000000, 0.0f, 0.0f},
    };

    // Create the vertex buffer.
    if( FAILED( pDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    // Fill the vertex buffer. We are setting the tu and tv texture
    // coordinates, which range from 0.0 to 1.0
    CUSTOMVERTEX* pVertices;
    if( FAILED( _pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
        return E_FAIL;
	memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    _pVB->Unlock();

    // Create the vertex buffer.
    if( FAILED( pDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &_pVB2, NULL ) ) )
    {
        return E_FAIL;
    }

    // Fill the vertex buffer. We are setting the tu and tv texture
    // coordinates, which range from 0.0 to 1.0
    //CUSTOMVERTEX* pVertices;
    if( FAILED( _pVB2->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
        return E_FAIL;
	memcpy( pVertices, g_Vertices2, sizeof(g_Vertices2) );
    _pVB2->Unlock();

	if ( !_filename.empty() )
		return loadTexture( pDevice );
	return S_OK;
}

/**
Chargement d'une texture
@param[in]	pDevice	pointeur sur un device D3D
*/
HRESULT XRectangle::loadTexture(LPDIRECT3DDEVICE9 pDevice)
{
	if ( _pTexture )
		_pTexture->Release();

	//Creation texture	
	if( FAILED( D3DXCreateTextureFromFile( pDevice, _filename.c_str(), &_pTexture ) ) )
	{
		return E_FAIL;
	}
	
	/*
	if( FAILED( D3DXCreateTextureFromFileEx( pDevice, _filename.c_str(), D3DX_DEFAULT, D3DX_DEFAULT,
		1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL, NULL, &_pTexture ) ) )
	{
		return E_FAIL;
	}
	
	D3DSURFACE_DESC desc;
	_pTexture->GetLevelDesc( 0, &desc );
	D3DLOCKED_RECT lockedRect;
	if(SUCCEEDED(_pTexture->LockRect(0, &lockedRect, NULL, D3DLOCK_DISCARD )))
	{
		DWORD *pByte = (DWORD*)lockedRect.pBits;
		for( DWORD y=0; y < desc.Height; y++ )
		{	
			DWORD dwOffset = y*desc.Width;
			for( DWORD x=0; x < desc.Width; x )
			{
				pByte[dwOffset+x] = 0x20FFFFFF ;
				x++;
			}
			_pTexture->UnlockRect(0);
		}
	}
	*/
	return S_OK;
}

/**
Met a jour la position et l'inclinaison de l'item
@param[in]	 x	position
@param[in]	 rot	inclinaison
*/
void XRectangle::set( const float& x, const float& rot)
{
	_x = x;
	_rot = rot * D3DX_PI / 180.0f ;
}

/**
Met a jour la position depuis celle d'un autre XRectangle
@param[in]	pRect pointeur sur le XRectangle de reference
*/
void XRectangle::set( const XRectangle* pRect )
{
	pRect->get( _x, _rot );
}

/**
Demande de transition vers la position d'un autre
rectangle
@param[in]	pRect	pointeur sur XRectangle destination
*/
void XRectangle::setToGo( const XRectangle* pRect )
{
	if ( pRect == NULL )
		_bToDel = true;
	else
	{
		pRect->get( _tx, _trot );
		_iTrans = 25;
		_txStep =  ( _tx - _x ) / 25.0f;
		_trotStep = ( _trot - _rot ) / 25.0f;
	}
}

/**
Recupere le x et l'inclinaison
@param[out]	x	position
@param[out]	rot inclinaison
*/
void XRectangle::get( float& x, float& rot ) const
{
	x = _x;
	rot = _rot;
}

/**
Translation 
@param[in]	pDevice	pointeur sur un device D3D
*/
void XRectangle::translate(LPDIRECT3DDEVICE9 pDevice)
{
	D3DXMATRIXA16 mat, matRotY, matTrans;
	D3DXMatrixTranslation( &matTrans, _x, 0.0f, 0.0f );
	D3DXMatrixRotationY( &matRotY, _rot );
	D3DXMatrixMultiply( &mat, &matRotY, &matTrans );
	pDevice->SetTransform( D3DTS_WORLD, &mat);
}

/**
Met a jour un event
@param	pEvent	pointeur sur un event
*/
void XRectangle::setEvent(EventPlace *pEvent)
{
	_pEventPlace = pEvent;
}

/**
Met a jour le nom de la image de la texture
@param[in]	filename	nom de la image de la texture
*/
void XRectangle::setImgFilename(const wchar_t *filename)
{
	_filename = filename;
}